class Register{
    constructor(){
        this.text=document.querySelector("input[type='text']")
        this.span=document.querySelector('.geshi')
        this.password=document.querySelector("input[type='password']")
        this.des=document.querySelector('.btn')
        this.Btn=document.querySelector('.Btn')

        // console.log(this.text,this.span,this.password,this.des,this.Btn)
        // console.log($)
        this.init()
    }
    init(){
        this.render()
    }
    render(){
      this.text.oninput=()=>{
        //   let username=this.text.value
          let telReg = /^1[3-9][0-9]{9}$/
          if(telReg.test(this.text.value)){
              this.span.innerHTML='手机格式正确'
              this.span.style.color='red'
          }else{
            this.span.innerHTML='手机格式错误'
            this.span.style.color='black'
            // return false
          }
      }

      this.password.oninput=()=>{
        //   let password=this.password.value
          let tel1Reg =/^(?=.*\d)(?=.*[A-z])[\da-zA-Z]{1,9}$/
          if(tel1Reg.test(this.password.value)){
            this.des.innerHTML='密码格式正确'
            this.des.style.color='red'
        }else{
          this.des.innerHTML='密码格式错误'
          this.des.style.color='black'
        //   return false
        }
      }
      this.Btn.onclick=()=>{
          let username=this.text.value
          let password=this.password.value
          if(username == '' || password == ''){
            alert('手机号密码不能为空')
            // return false
          }
          this.request(username,password)
      }
    }
    request(username,password){
        $.ajax({
            url: 'http://api.yuguoxy.com/api/member/add',
            type: 'post',
            data:{
                username,
                password
            }
        })
        .then(res=>{
            if(this.des.innerHTML=='密码格式错误'){
                return false
            }
            // console.log(res)
            if(res.resultCode == 1){
                alert('注册成功')
                if(confirm('去登陆吗?')){
                    location.href='../html/login.html'
                }

            }
        })

    }

    

    

}
new Register